package com.major.search.sort;

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        bubbleSort(new int[]{2, 4, 5, 1, 3});
    }

    /**
     * 冒泡排序：
     * @param arr
     */
    static void bubbleSort(int[] arr) {
        int temp = 0;
        boolean unCompared = true ;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    unCompared = false;
                    temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
            if (unCompared){
                break;
            }else {
                unCompared = true;
            }
        }

        System.out.println(Arrays.toString(arr));
    }
}
